package ReusableCode;

import java.util.*;
import java.util.*;

public class GeneralFunctions {

	public static List<long[]> generatePascal(int length)
	{
		List<long[]> pascals = new ArrayList<long[]>();
		
		for(int i = 0; i < length; i++)
		{
			long row[] = new long[i+1];
			long prev[] = new long[i+1]; 
			
			if(i>0)
			{
				prev = pascals.get(i-1);
			}
			
			for(int j = 0; j < i+1; j++)
			{
				if(j==0 || j==(row.length-1))
				{
					row[j] = 1;
				}
				else
				{
					row[j] = prev[j-1] + prev[j];
				}
			}
			
			pascals.add(row);
		}
		
		return pascals;
	}
	
	public static List<Long> generatePentagonNumbers(long max)
	{
		List<Long> nums = new ArrayList<Long>();
		
		long num=0, n=1;
		while (num <= max)
		{
			if(num>0)
			{
				nums.add(num);
			}
			
			num = ((n * ((3 * n)-1))/2);
			n++;
		}
		
		return nums;
	}
	
	public static HashSet<Long> generatePentagonNumbersHash(long max)
	{
		HashSet<Long> nums = new HashSet<Long>();
		
		long num=0, n=1;
		while (num <= max)
		{
			if(num>0)
			{
				nums.add(num);
			}
			
			num = ((n * ((3 * n)-1))/2);
			n++;
		}
		
		return nums;
	}
	 
	public static boolean isPermutation(long a, long b)
	{
		int num1[] = MathFunctions.getDigits(a);
		int num2[] = MathFunctions.getDigits(b);
		int digs1[] = new int[10];
		int digs2[] = new int[10];
		
		if(num1.length!=num2.length)
		{
			return false;
		}
		
		//Zero the arrays.
		for(int i=0; i<10; i++)
		{
			digs1[i] = 0;
			digs2[i] = 0;
		}
		
		for(int i=0; i<num1.length; i++)
		{
			digs1[num1[i]] = digs1[num1[i]] + 1;
			digs2[num2[i]] = digs2[num2[i]] + 1;
		}
		
		for(int i=0; i<10; i++)
		{
			if(digs1[i]!=digs2[i])
			{
				return false;
			}
		}
		
		return true;
	}
	
	public static long getWordScore(String name)
	{
		long score = 0;
		String temp = "";
		for(int i = 0; i < name.length(); i++)
		{
			int num = (name.charAt(i))-64;
			temp += num + " ";
			score += num;
		}
		//System.out.println(name + ": " + temp + "= " + score);
		
		return score;
	}
	
	public static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    
	    return true;
	}
}
